package com.heima.admin.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.heima.admin.mapper.ChannelMapper;
import com.heima.admin.service.ChannelService;
import com.heima.common.exception.CustException;
import com.heima.common.exception.CustomException;
import com.heima.model.admin.dtos.ChannelDTO;
import com.heima.model.admin.pojos.AdChannel;
import com.heima.model.common.dtos.PageResponseResult;
import com.heima.model.common.dtos.ResponseResult;
import com.heima.model.common.enums.AppHttpCodeEnum;
import org.apache.commons.lang3.StringUtils;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.Date;

@Service
public class ChannelServiceImpl extends ServiceImpl<ChannelMapper, AdChannel> implements ChannelService {


    @Override
    public PageResponseResult findByQuery(ChannelDTO dto) {
        //1. 参数校验代码
        dto.checkParam();

        //2. 分页查询
        Page<AdChannel> pageReq = new Page<>(dto.getPage(), dto.getSize());
        //2.1 封装分页查询条件
        LambdaQueryWrapper<AdChannel> queryWrapper = Wrappers.lambdaQuery();
        if (StringUtils.isNotBlank(dto.getName())) {
            queryWrapper.like(AdChannel::getName, dto.getName());
        }
        if (dto.getStatus() != null) {
            queryWrapper.eq(AdChannel::getStatus, dto.getStatus());
        }
        queryWrapper.orderByAsc(AdChannel::getOrd);

        //2.2 其他查询条件
        // 参数1 分页条件 参数2 查询条件
        //3 执行查询操作
        IPage<AdChannel> pageResult = this.page(pageReq, queryWrapper);

        //封装结果返回
        return new PageResponseResult(dto.getPage(), dto.getSize(), pageResult.getTotal(), pageResult.getRecords());
    }

    @Override
    public ResponseResult insert(AdChannel adChannel) {
        //1. 校验参数
        //1.1. 名字不能为空
        String name = adChannel.getName();
        if (StringUtils.isBlank(name)) {
            return ResponseResult.errorResult(AppHttpCodeEnum.PARAM_INVALID, "频道名称不能为空");
        }
        //1.2 名称长度不能大于10
        if (name.length() > 10) {
            return ResponseResult.errorResult(AppHttpCodeEnum.PARAM_INVALID, "频道名称长度不能大于10");
        }
        //1.3 校验频道名称不能为重复
        int count = this.count(Wrappers.<AdChannel>lambdaQuery().eq(AdChannel::getName, name));
        if (count > 0) {
            return ResponseResult.errorResult(AppHttpCodeEnum.DATA_EXIST, "频道已经存在");
        }
        //2 将频道保存到数据库
        adChannel.setCreatedTime(new Date());
        this.save(adChannel);
        //3 放回成功的响应
        return ResponseResult.okResult();
    }

    @Override
    public ResponseResult update(AdChannel adChannel) {
        //1  参数校验
        //1.1 检查频道id
        if (adChannel.getId() == null) {
            return ResponseResult.errorResult(AppHttpCodeEnum.PARAM_INVALID, "频道id不能为空");
        }
        //1.2 根据id查询频道信息
        AdChannel oldChannl = getById(adChannel.getId());
        if (oldChannl == null) {
            return ResponseResult.errorResult(AppHttpCodeEnum.DATA_NOT_EXIST, "频道不存在");
        }
        //1.3 判断参数name是否为空 如果频道信息不为空 判断和原来的频道id是否一致
        String name = adChannel.getName();
        //频道名称不能为空  并且频道名称做了改变 需要校验一下频道名称
        if (StringUtils.isNotBlank(name) && !name.equals(oldChannl.getName())) {
            //名称长度不能大于10
            if (name.length() > 10) {
                return ResponseResult.errorResult(AppHttpCodeEnum.PARAM_INVALID, "频道名称长度不能大于10");
            }
            //校验频道名称不能为重复
            int count = this.count(Wrappers.<AdChannel>lambdaQuery().eq(AdChannel::getName, name));
            if (count > 0) {
                return ResponseResult.errorResult(AppHttpCodeEnum.DATA_EXIST, "频道已经存在");
            }
        }
        //2  修改频道
        updateById(adChannel);
        //3  响应结果
        return ResponseResult.okResult();
    }

    @Transactional
    @Override
    public ResponseResult deleteById(Integer id) {
        //1. 校验参数
        // 根据id查询频道
//        try {
        AdChannel channel = getById(id);

        if (channel == null) {
            return ResponseResult.errorResult(AppHttpCodeEnum.DATA_NOT_EXIST, "频道已经不存在");
        }
        //判断频道是否启用
        if (channel.getStatus()) {
            return ResponseResult.errorResult(AppHttpCodeEnum.DATA_NOT_ALLOW, "频道有效,不允许删除");
        }
        //2 如果未启用删除频道
        removeById(id);
//        System.out.println(1 / 0);
//        } catch (Exception e) {
//            e.printStackTrace();
//            return ResponseResult.errorResult(AppHttpCodeEnum.SERVER_ERROR, "服务器出现异常，请稍后重试");
//        }

        if (id == 50) {
            CustException.cust(AppHttpCodeEnum.DATA_NOT_ALLOW);
        }

        //3 返回响应结果
        return ResponseResult.okResult();
    }

}
